﻿@using MarketPulse.BusinessLogic.Private;
@using (Html.BeginForm("UpdateAssignment", "Assignment", FormMethod.Post))
{

<input type="hidden" name="AssignmentGuid" value="@ViewBag.Assignment.GUID" />
<input type="hidden" name="AssignmentVersionId" value="@ViewBag.Assignment.VersionId" />

<!-- Start View Container Section -->
<div id="view-container">

    <!-- Start Top Container Section -->
    <div id="top-container">

        <!-- Start Left Container Section -->
        <div id="left-container">

            <!-- Start Left Top Container Section -->
            <div id="left-top-container">
                <div id="branch-details">
                    <div id="branch-left">
                        <img src="@Url.Content("~/Content/Images/mcdo.jpg")" alt="@ViewBag.Assignment.Branch.Name" />
                    </div>
                    <div id="branch-right">
                        <div class="inputControls">
                            <div class="inputControlsRight">
                                <label class="title" title="Address">Address:</label>
                                @if (ViewBag.Assignment.Branch.AddressLine1 != null)
                                {
                                    <span>
                                        @ViewBag.Assignment.Branch.AddressLine1
                                    </span>
                                }
                                @if (ViewBag.Assignment.Branch.AddressLine2 != null)
                                {                                
                                    <span>
                                        @ViewBag.Assignment.Branch.AddressLine2
                                    </span>
                                }
                                @if (ViewBag.Assignment.Branch.AddressLine3 != null)
                                {                                
                                    <span>
                                        @ViewBag.Assignment.Branch.AddressLine3
                                    </span>
                                }
                                @if (ViewBag.Assignment.Branch.AddressLine4 != null)
                                {                                
                                    <span>
                                        @ViewBag.Assignment.Branch.AddressLine4
                                    </span>
                                }
                                @if (ViewBag.Assignment.Branch.AddressLine5 != null)
                                {                                
                                    <span>
                                        @ViewBag.Assignment.Branch.AddressLine5
                                    </span>
                                }
                            </div>
                            <div class="clear"></div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- End Left Top Container Section -->

            <!-- Start Left Middle Container Section -->
            <div id="left-middle-container">
                <div class="left">
                    <label class="title" title="Select Valid Date:">Select Date:</label>
                    <div id="datepicker" class="assignment-calendar"></div>
                    <div class="clear"></div>
                </div>
                <div class="right"> 
                    <label class="title" title="Select Valid Time:">Select Time:</label>
                    <select id="timelist" name="timelist" class="time-list" multiple="multiple">
                        @{
                            DateTime assignmentDate;

                            assignmentDate = ViewBag.Assignment.ValidStartDateTime;
                            TimeSpan currentTime = assignmentDate.TimeOfDay;

                            assignmentDate = ViewBag.Assignment.ValidEndDateTime;
                            TimeSpan endTime = assignmentDate.TimeOfDay;

                            // Break valid times by half hour
                            while (currentTime <= endTime)
                            {
                                assignmentDate = new DateTime(currentTime.Ticks);
                                <option>@assignmentDate.ToString("HH:mm")</option>
                                currentTime = currentTime.Add(TimeSpan.FromMinutes(30));
                            }
                        }
                    </select>
                </div>
                <input id="plannedDate" name="plannedDate" type="hidden" value="@ViewBag.Assignment.ValidStartDateTime.ToString("MM/dd/yyyy")" />
                <div class="clear"></div>
            </div>
            <!-- End Left Middle Container Section -->

            <!-- Start Left Bottom Container Section -->
            <div id="left-bottom-container">
                <label class="title" title="Payments">Payments</label>
                <div class="payment-container">
                    @if (ViewBag.Assignment.Payments.Count == 0)
                    {
                        <span>There are no payments available.</span>
                    }
                    else
                    {
                        foreach (AssignmentPayment payment in ViewBag.Assignment.Payments)
                        {
                            <div class="container-row">
                                <span class="payment-type">
                                    @ViewBag.PaymentTypeInfoList.FindByGuid(payment.AssignmentPaymentTypeGuid.Value).Label:
                                </span>
                                <span class="payment-detail">
                                    @String.Format("{0:C}", Convert.ToInt32(payment.Amount))
                                </span>
                                <span class="payment-detail">
                                    <input id="@payment.GUID" name="@payment.GUID" type="text" />
                                </span>
                            </div>
                        }
                    }
                </div>
            </div>
            <!-- End Left Bottom Container Section -->

        </div>
        <!-- End Left Container Section -->

        <!-- Start Right Container Section -->
        <div id="right-container">
            <label class="title" title="Shoppers">Select Shopper</label>
            <div class="table-container">
                @if (ViewBag.Shoppers.Count == 0)
                {
                    <span>There are no shoppers available.</span>
                }
                else
                {
                    <table id="shoppers">
                        <tr>
                            <th>Name</th>
                            <th>Email</th>
                        </tr>
                
                        @foreach (var shopper in ViewBag.Shoppers)
                        {
                            <tr id="@shopper.GUID" onclick="javascript:SetUser(this);">
                                <td>
                                    @shopper.FullName.ToString()
                                </td>
                                <td>
                                    @shopper.EMail
                                </td>
                            </tr>                    
                        }
                    </table>
                }
            </div>
            <input id="assignedUser" name="assignedUser" type="hidden" />
        </div>
        <!-- End Right Container Section -->

        <div class="clear"></div>
    </div>
    <!-- End Top Container Section -->

    <!-- Start Bottom Container Section -->
    <div id="bottom-container">

        <div id="button-container">
            <input type="button" value="Cancel" onclick="javascript:DestroyModalDialog();"/>
            <input type="submit" value="Save"/>

            <div class="clear"></div>
        </div>

    </div>
    <!-- End Bottom Container Section -->

</div>
<!-- End View Container Section -->
}

<script type="text/javascript">

    $(document).ready(function () {

        $('#datepicker').datepicker({
            defaultDate: '@ViewBag.Assignment.ValidStartDateTime',
            minDate: '@ViewBag.Assignment.ValidStartDateTime',
            maxDate: '@ViewBag.Assignment.ValidEndDateTime',
            onSelect: function (date) {
                SetVisitDate(date)
            }
        });

        // Set the current selected date
        $('#datepicker').datepicker("setDate", '@ViewBag.Assignment.PlannedDateTime');

        $("#timelist option:first").attr('selected', 'selected');

        $("#timelist option[selected]").removeAttr("selected");
        $("select#cboDays option[value='Wednesday']").attr("selected", "selected"); 

        $("#shoppers tr:nth-child(2)").click();

        if (!$("#shoppers").length || !$(".container-row").length)
        {
            $("input[type=submit]").attr("disabled", "disabled");
        }
    });

</script>